static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
                                           struct tcf_chain_info *chain_info,
                                           u32 protocol, u32 prio,
-                                          bool prio_allocate);
+                                          bool prio_allocate,
+                                          struct netlink_ext_ack *extack);
 
 /* Try to insert new proto.
  * If proto with specified priority already exists, free new proto
                return ERR_PTR(-EAGAIN);
        }
 
-       tp = tcf_chain_tp_find(chain, &chain_info,
-                              protocol, prio, false);
+       tp = tcf_chain_tp_find(chain, &chain_info, protocol, prio, false, NULL);
        if (!tp)
                err = tcf_chain_tp_insert(chain, &chain_info, tp_new);
        mutex_unlock(&chain->filter_chain_lock);
 static struct tcf_proto *tcf_chain_tp_find(struct tcf_chain *chain,
                                           struct tcf_chain_info *chain_info,
                                           u32 protocol, u32 prio,
-                                          bool prio_allocate)
+                                          bool prio_allocate,
+                                          struct netlink_ext_ack *extack)
 {
        struct tcf_proto **pprev;
        struct tcf_proto *tp;
             pprev = &tp->next) {
                if (tp->prio >= prio) {
                        if (tp->prio == prio) {
-                               if (prio_allocate ||
-                                   (tp->protocol != protocol && protocol))
+                               if (prio_allocate) {
+                                       NL_SET_ERR_MSG(extack, "Lowest ID from auto-alloc range already in use");
+                                       return ERR_PTR(-ENOSPC);
+                               }
+                               if (tp->protocol != protocol && protocol) {
+                                       NL_SET_ERR_MSG(extack, "Protocol mismatch for filter with specified priority");
                                        return ERR_PTR(-EINVAL);
+                               }
                        } else {
                                tp = NULL;
                        }
 
        mutex_lock(&chain->filter_chain_lock);
        tp = tcf_chain_tp_find(chain, &chain_info, protocol,
-                              prio, prio_allocate);
+                              prio, prio_allocate, extack);
        if (IS_ERR(tp)) {
-               NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
                err = PTR_ERR(tp);
                goto errout_locked;
        }
 
        mutex_lock(&chain->filter_chain_lock);
        tp = tcf_chain_tp_find(chain, &chain_info, protocol,
-                              prio, false);
-       if (!tp || IS_ERR(tp)) {
+                              prio, false, extack);
+       if (!tp) {
+               err = -ENOENT;
                NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
-               err = tp ? PTR_ERR(tp) : -ENOENT;
+               goto errout_locked;
+       } else if (IS_ERR(tp)) {
+               err = PTR_ERR(tp);
                goto errout_locked;
        } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
                NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");
 
        mutex_lock(&chain->filter_chain_lock);
        tp = tcf_chain_tp_find(chain, &chain_info, protocol,
-                              prio, false);
+                              prio, false, extack);
        mutex_unlock(&chain->filter_chain_lock);
-       if (!tp || IS_ERR(tp)) {
+       if (!tp) {
+               err = -ENOENT;
                NL_SET_ERR_MSG(extack, "Filter with specified priority/protocol not found");
-               err = tp ? PTR_ERR(tp) : -ENOENT;
+               goto errout;
+       } else if (IS_ERR(tp)) {
+               err = PTR_ERR(tp);
                goto errout;
        } else if (tca[TCA_KIND] && nla_strcmp(tca[TCA_KIND], tp->ops->kind)) {
                NL_SET_ERR_MSG(extack, "Specified filter kind does not match existing one");