Lines Matching +full:- +full:dig +full:- +full:div
1 // SPDX-License-Identifier: GPL-2.0-or-later
7 * PACKET - implements raw packet sockets.
22 * Alan Cox : Re-commented the code.
30 * Alan Cox : New buffers. Use sk->mac.raw.
101 - If the device has no dev->header_ops->create, there is no LL header
108 needed_headroom to be (the real WiFi header length - the fake Ethernet
110 - packet socket receives packets with pulled ll header,
114 -----------
117 mac_header -> ll header
118 data -> data
121 mac_header -> ll header
122 data -> ll header
125 mac_header -> data
128 data -> data
131 mac_header -> data. ll header is invisible to us.
132 data -> data
140 ------------
143 mac_header -> ll header
144 data -> ll header
147 mac_header -> data
148 data -> data
183 #define BLOCK_STATUS(x) ((x)->hdr.bh1.block_status)
184 #define BLOCK_NUM_PKTS(x) ((x)->hdr.bh1.num_pkts)
185 #define BLOCK_O2FP(x) ((x)->hdr.bh1.offset_to_first_pkt)
186 #define BLOCK_LEN(x) ((x)->hdr.bh1.blk_len)
187 #define BLOCK_SNUM(x) ((x)->hdr.bh1.seq_num)
188 #define BLOCK_O2PRIV(x) ((x)->offset_to_priv)
232 #define PACKET_SKB_CB(__skb) ((struct packet_skb_cb *)((__skb)->cb))
234 #define GET_PBDQC_FROM_RB(x) ((struct tpacket_kbdq_core *)(&(x)->prb_bdqc))
236 ((struct tpacket_block_desc *)((x)->pkbdq[(bid)].buffer))
238 ((struct tpacket_block_desc *)((x)->pkbdq[(x)->kactive_blk_num].buffer))
240 (((x)->kactive_blk_num < ((x)->knum_blocks-1)) ? \
241 ((x)->kactive_blk_num+1) : 0)
254 next = skb->next; in nf_hook_direct_egress()
257 if (!nf_hook_egress(skb, &rc, skb->dev)) in nf_hook_direct_egress()
263 tail->next = skb; in nf_hook_direct_egress()
290 dev = rcu_dereference(po->cached_dev); in packet_cached_dev_get()
300 rcu_assign_pointer(po->cached_dev, dev); in packet_cached_dev_assign()
305 RCU_INIT_POINTER(po->cached_dev, NULL); in packet_cached_dev_reset()
310 return po->xmit == packet_direct_xmit; in packet_use_direct_xmit()
315 struct net_device *dev = skb->dev; in packet_pick_tx_queue()
316 const struct net_device_ops *ops = dev->netdev_ops; in packet_pick_tx_queue()
321 skb->sender_cpu = cpu + 1; in packet_pick_tx_queue()
323 skb_record_rx_queue(skb, cpu % dev->real_num_tx_queues); in packet_pick_tx_queue()
324 if (ops->ndo_select_queue) { in packet_pick_tx_queue()
325 queue_index = ops->ndo_select_queue(dev, skb, NULL); in packet_pick_tx_queue()
342 if (!po->running) { in __register_prot_hook()
343 if (po->fanout) in __register_prot_hook()
346 dev_add_pack(&po->prot_hook); in __register_prot_hook()
349 po->running = 1; in __register_prot_hook()
355 lockdep_assert_held_once(&pkt_sk(sk)->bind_lock); in register_prot_hook()
360 * the po->bind_lock and do a synchronize_net to make sure no
362 * of po->prot_hook. If the sync parameter is false, it is the
369 lockdep_assert_held_once(&po->bind_lock); in __unregister_prot_hook()
371 po->running = 0; in __unregister_prot_hook()
373 if (po->fanout) in __unregister_prot_hook()
376 __dev_remove_pack(&po->prot_hook); in __unregister_prot_hook()
381 spin_unlock(&po->bind_lock); in __unregister_prot_hook()
383 spin_lock(&po->bind_lock); in __unregister_prot_hook()
391 if (po->running) in unregister_prot_hook()
407 switch (po->tp_version) { in __packet_set_status()
409 h.h1->tp_status = status; in __packet_set_status()
410 flush_dcache_page(pgv_to_page(&h.h1->tp_status)); in __packet_set_status()
413 h.h2->tp_status = status; in __packet_set_status()
414 flush_dcache_page(pgv_to_page(&h.h2->tp_status)); in __packet_set_status()
417 h.h3->tp_status = status; in __packet_set_status()
418 flush_dcache_page(pgv_to_page(&h.h3->tp_status)); in __packet_set_status()
435 switch (po->tp_version) { in __packet_get_status()
437 flush_dcache_page(pgv_to_page(&h.h1->tp_status)); in __packet_get_status()
438 return h.h1->tp_status; in __packet_get_status()
440 flush_dcache_page(pgv_to_page(&h.h2->tp_status)); in __packet_get_status()
441 return h.h2->tp_status; in __packet_get_status()
443 flush_dcache_page(pgv_to_page(&h.h3->tp_status)); in __packet_get_status()
444 return h.h3->tp_status; in __packet_get_status()
459 ktime_to_timespec64_cond(shhwtstamps->hwtstamp, ts)) in tpacket_get_timestamp()
476 if (!(ts_status = tpacket_get_timestamp(skb, &ts, po->tp_tstamp))) in __packet_set_timestamp()
482 * all store the seconds in a 32-bit unsigned integer. in __packet_set_timestamp()
483 * If we create a version 4, that should have a 64-bit timestamp, in __packet_set_timestamp()
484 * either 64-bit seconds + 32-bit nanoseconds, or just 64-bit in __packet_set_timestamp()
487 switch (po->tp_version) { in __packet_set_timestamp()
489 h.h1->tp_sec = ts.tv_sec; in __packet_set_timestamp()
490 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC; in __packet_set_timestamp()
493 h.h2->tp_sec = ts.tv_sec; in __packet_set_timestamp()
494 h.h2->tp_nsec = ts.tv_nsec; in __packet_set_timestamp()
497 h.h3->tp_sec = ts.tv_sec; in __packet_set_timestamp()
498 h.h3->tp_nsec = ts.tv_nsec; in __packet_set_timestamp()
506 flush_dcache_page(pgv_to_page(&h.h1->tp_sec)); in __packet_set_timestamp()
520 pg_vec_pos = position / rb->frames_per_block; in packet_lookup_frame()
521 frame_offset = position % rb->frames_per_block; in packet_lookup_frame()
523 h.raw = rb->pg_vec[pg_vec_pos].buffer + in packet_lookup_frame()
524 (frame_offset * rb->frame_size); in packet_lookup_frame()
536 return packet_lookup_frame(po, rb, rb->head, status); in packet_current_frame()
541 del_timer_sync(&pkc->retire_blk_timer); in prb_del_retire_blk_timer()
549 pkc = GET_PBDQC_FROM_RB(&po->rx_ring); in prb_shutdown_retire_blk_timer()
551 spin_lock_bh(&rb_queue->lock); in prb_shutdown_retire_blk_timer()
552 pkc->delete_blk_timer = 1; in prb_shutdown_retire_blk_timer()
553 spin_unlock_bh(&rb_queue->lock); in prb_shutdown_retire_blk_timer()
562 pkc = GET_PBDQC_FROM_RB(&po->rx_ring); in prb_setup_retire_blk_timer()
563 timer_setup(&pkc->retire_blk_timer, prb_retire_rx_blk_timer_expired, in prb_setup_retire_blk_timer()
565 pkc->retire_blk_timer.expires = jiffies; in prb_setup_retire_blk_timer()
572 unsigned int mbits, div; in prb_calc_retire_blk_tmo() local
577 dev = __dev_get_by_index(sock_net(&po->sk), po->ifindex); in prb_calc_retire_blk_tmo()
594 div = ecmd.base.speed / 1000; in prb_calc_retire_blk_tmo()
597 if (div) in prb_calc_retire_blk_tmo()
598 mbits /= div; in prb_calc_retire_blk_tmo()
600 if (div) in prb_calc_retire_blk_tmo()
608 p1->feature_req_word = req_u->req3.tp_feature_req_word; in prb_init_ft_ops()
621 p1->knxt_seq_num = 1; in init_prb_bdqc()
622 p1->pkbdq = pg_vec; in init_prb_bdqc()
624 p1->pkblk_start = pg_vec[0].buffer; in init_prb_bdqc()
625 p1->kblk_size = req_u->req3.tp_block_size; in init_prb_bdqc()
626 p1->knum_blocks = req_u->req3.tp_block_nr; in init_prb_bdqc()
627 p1->hdrlen = po->tp_hdrlen; in init_prb_bdqc()
628 p1->version = po->tp_version; in init_prb_bdqc()
629 p1->last_kactive_blk_num = 0; in init_prb_bdqc()
630 po->stats.stats3.tp_freeze_q_cnt = 0; in init_prb_bdqc()
631 if (req_u->req3.tp_retire_blk_tov) in init_prb_bdqc()
632 p1->retire_blk_tov = req_u->req3.tp_retire_blk_tov; in init_prb_bdqc()
634 p1->retire_blk_tov = prb_calc_retire_blk_tmo(po, in init_prb_bdqc()
635 req_u->req3.tp_block_size); in init_prb_bdqc()
636 p1->tov_in_jiffies = msecs_to_jiffies(p1->retire_blk_tov); in init_prb_bdqc()
637 p1->blk_sizeof_priv = req_u->req3.tp_sizeof_priv; in init_prb_bdqc()
638 rwlock_init(&p1->blk_fill_in_prog_lock); in init_prb_bdqc()
640 p1->max_frame_len = p1->kblk_size - BLK_PLUS_PRIV(p1->blk_sizeof_priv); in init_prb_bdqc()
651 mod_timer(&pkc->retire_blk_timer, in _prb_refresh_rx_retire_blk_timer()
652 jiffies + pkc->tov_in_jiffies); in _prb_refresh_rx_retire_blk_timer()
653 pkc->last_kactive_blk_num = pkc->kactive_blk_num; in _prb_refresh_rx_retire_blk_timer()
660 * on packet-by-packet basis.
662 * With a 1MB block-size, on a 1Gbps line, it will take
675 * a) line-speed and b) block-size.
683 struct tpacket_kbdq_core *pkc = GET_PBDQC_FROM_RB(&po->rx_ring); in prb_retire_rx_blk_timer_expired()
687 spin_lock(&po->sk.sk_receive_queue.lock); in prb_retire_rx_blk_timer_expired()
692 if (unlikely(pkc->delete_blk_timer)) in prb_retire_rx_blk_timer_expired()
706 write_lock(&pkc->blk_fill_in_prog_lock); in prb_retire_rx_blk_timer_expired()
707 write_unlock(&pkc->blk_fill_in_prog_lock); in prb_retire_rx_blk_timer_expired()
710 if (pkc->last_kactive_blk_num == pkc->kactive_blk_num) { in prb_retire_rx_blk_timer_expired()
722 /* Case 1. Queue was frozen because user-space was in prb_retire_rx_blk_timer_expired()
727 * Ok, user-space is still behind. in prb_retire_rx_blk_timer_expired()
732 /* Case 2. queue was frozen,user-space caught up, in prb_retire_rx_blk_timer_expired()
737 * Thawing/timer-refresh is a side effect. in prb_retire_rx_blk_timer_expired()
749 spin_unlock(&po->sk.sk_receive_queue.lock); in prb_retire_rx_blk_timer_expired()
765 end = (u8 *)PAGE_ALIGN((unsigned long)pkc1->pkblk_end); in prb_flush_block()
802 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1; in prb_close_block()
803 struct sock *sk = &po->sk; in prb_close_block()
805 if (atomic_read(&po->tp_drops)) in prb_close_block()
808 last_pkt = (struct tpacket3_hdr *)pkc1->prev; in prb_close_block()
809 last_pkt->tp_next_offset = 0; in prb_close_block()
813 h1->ts_last_pkt.ts_sec = last_pkt->tp_sec; in prb_close_block()
814 h1->ts_last_pkt.ts_nsec = last_pkt->tp_nsec; in prb_close_block()
816 /* Ok, we tmo'd - so get the current time. in prb_close_block()
823 h1->ts_last_pkt.ts_sec = ts.tv_sec; in prb_close_block()
824 h1->ts_last_pkt.ts_nsec = ts.tv_nsec; in prb_close_block()
832 sk->sk_data_ready(sk); in prb_close_block()
834 pkc1->kactive_blk_num = GET_NEXT_PRB_BLK_NUM(pkc1); in prb_close_block()
839 pkc->reset_pending_on_curr_blk = 0; in prb_thaw_queue()
853 struct tpacket_hdr_v1 *h1 = &pbd1->hdr.bh1; in prb_open_block()
861 BLOCK_SNUM(pbd1) = pkc1->knxt_seq_num++; in prb_open_block()
863 BLOCK_LEN(pbd1) = BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); in prb_open_block()
867 h1->ts_first_pkt.ts_sec = ts.tv_sec; in prb_open_block()
868 h1->ts_first_pkt.ts_nsec = ts.tv_nsec; in prb_open_block()
870 pkc1->pkblk_start = (char *)pbd1; in prb_open_block()
871 pkc1->nxt_offset = pkc1->pkblk_start + BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); in prb_open_block()
873 BLOCK_O2FP(pbd1) = (__u32)BLK_PLUS_PRIV(pkc1->blk_sizeof_priv); in prb_open_block()
876 pbd1->version = pkc1->version; in prb_open_block()
877 pkc1->prev = pkc1->nxt_offset; in prb_open_block()
878 pkc1->pkblk_end = pkc1->pkblk_start + pkc1->kblk_size; in prb_open_block()
891 * 4) user-space is either sleeping or processing block '0'.
893 * it will close block-7,loop around and try to fill block '0'.
894 * call-flow:
898 * |->(BLOCK_STATUS == USER) evaluates to true
899 * 5.1) Since block-0 is currently in-use, we just freeze the queue.
904 * re-open block-0 in near future.
906 * case and __packet_lookup_frame_in_block will check if block-0
907 * is free and can now be re-used.
912 pkc->reset_pending_on_curr_blk = 1; in prb_freeze_queue()
913 po->stats.stats3.tp_freeze_q_cnt++; in prb_freeze_queue()
946 return (void *)pkc->nxt_offset; in prb_dispatch_next_block()
958 * cpu-0 and tpacket_rcv() got invoked on cpu-1, didn't in prb_retire_current_block()
963 * the timer-handler already handled this case. in prb_retire_current_block()
967 write_lock(&pkc->blk_fill_in_prog_lock); in prb_retire_current_block()
968 write_unlock(&pkc->blk_fill_in_prog_lock); in prb_retire_current_block()
982 return pkc->reset_pending_on_curr_blk; in prb_queue_frozen()
986 __releases(&pkc->blk_fill_in_prog_lock) in prb_clear_blk_fill_status()
990 read_unlock(&pkc->blk_fill_in_prog_lock); in prb_clear_blk_fill_status()
996 ppd->hv1.tp_rxhash = skb_get_hash(pkc->skb); in prb_fill_rxhash()
1002 ppd->hv1.tp_rxhash = 0; in prb_clear_rxhash()
1008 if (skb_vlan_tag_present(pkc->skb)) { in prb_fill_vlan_info()
1009 ppd->hv1.tp_vlan_tci = skb_vlan_tag_get(pkc->skb); in prb_fill_vlan_info()
1010 ppd->hv1.tp_vlan_tpid = ntohs(pkc->skb->vlan_proto); in prb_fill_vlan_info()
1011 ppd->tp_status = TP_STATUS_VLAN_VALID | TP_STATUS_VLAN_TPID_VALID; in prb_fill_vlan_info()
1013 ppd->hv1.tp_vlan_tci = 0; in prb_fill_vlan_info()
1014 ppd->hv1.tp_vlan_tpid = 0; in prb_fill_vlan_info()
1015 ppd->tp_status = TP_STATUS_AVAILABLE; in prb_fill_vlan_info()
1022 ppd->hv1.tp_padding = 0; in prb_run_all_ft_ops()
1025 if (pkc->feature_req_word & TP_FT_REQ_FILL_RXHASH) in prb_run_all_ft_ops()
1035 __acquires(&pkc->blk_fill_in_prog_lock) in prb_fill_curr_block()
1040 ppd->tp_next_offset = TOTAL_PKT_LEN_INCL_ALIGN(len); in prb_fill_curr_block()
1041 pkc->prev = curr; in prb_fill_curr_block()
1042 pkc->nxt_offset += TOTAL_PKT_LEN_INCL_ALIGN(len); in prb_fill_curr_block()
1045 read_lock(&pkc->blk_fill_in_prog_lock); in prb_fill_curr_block()
1049 /* Assumes caller has the sk->rx_queue.lock */
1059 pkc = GET_PBDQC_FROM_RB(&po->rx_ring); in __packet_lookup_frame_in_block()
1066 * is still in_use by user-space. in __packet_lookup_frame_in_block()
1073 * Ok, the block was released by user-space. in __packet_lookup_frame_in_block()
1083 curr = pkc->nxt_offset; in __packet_lookup_frame_in_block()
1084 pkc->skb = skb; in __packet_lookup_frame_in_block()
1085 end = (char *)pbd + pkc->kblk_size; in __packet_lookup_frame_in_block()
1116 switch (po->tp_version) { in packet_current_rx_frame()
1119 curr = packet_lookup_frame(po, &po->rx_ring, in packet_current_rx_frame()
1120 po->rx_ring.head, status); in packet_current_rx_frame()
1147 if (rb->prb_bdqc.kactive_blk_num) in prb_previous_blk_num()
1148 prev = rb->prb_bdqc.kactive_blk_num-1; in prb_previous_blk_num()
1150 prev = rb->prb_bdqc.knum_blocks-1; in prb_previous_blk_num()
1167 if (po->tp_version <= TPACKET_V2) in packet_previous_rx_frame()
1176 switch (po->tp_version) { in packet_increment_rx_head()
1192 unsigned int previous = rb->head ? rb->head - 1 : rb->frame_max; in packet_previous_frame()
1198 buff->head = buff->head != buff->frame_max ? buff->head+1 : 0; in packet_increment_head()
1203 this_cpu_inc(*rb->pending_refcnt); in packet_inc_pending()
1208 this_cpu_dec(*rb->pending_refcnt); in packet_dec_pending()
1217 if (rb->pending_refcnt == NULL) in packet_read_pending()
1221 refcnt += *per_cpu_ptr(rb->pending_refcnt, cpu); in packet_read_pending()
1228 po->rx_ring.pending_refcnt = NULL; in packet_alloc_pending()
1230 po->tx_ring.pending_refcnt = alloc_percpu(unsigned int); in packet_alloc_pending()
1231 if (unlikely(po->tx_ring.pending_refcnt == NULL)) in packet_alloc_pending()
1232 return -ENOBUFS; in packet_alloc_pending()
1239 free_percpu(po->tx_ring.pending_refcnt); in packet_free_pending()
1251 len = READ_ONCE(po->rx_ring.frame_max) + 1; in __tpacket_has_room()
1252 idx = READ_ONCE(po->rx_ring.head); in __tpacket_has_room()
1256 idx -= len; in __tpacket_has_room()
1257 return packet_lookup_frame(po, &po->rx_ring, idx, TP_STATUS_KERNEL); in __tpacket_has_room()
1264 len = READ_ONCE(po->rx_ring.prb_bdqc.knum_blocks); in __tpacket_v3_has_room()
1265 idx = READ_ONCE(po->rx_ring.prb_bdqc.kactive_blk_num); in __tpacket_v3_has_room()
1269 idx -= len; in __tpacket_v3_has_room()
1270 return prb_lookup_block(po, &po->rx_ring, idx, TP_STATUS_KERNEL); in __tpacket_v3_has_room()
1276 const struct sock *sk = &po->sk; in __packet_rcv_has_room()
1279 if (po->prot_hook.func != tpacket_rcv) { in __packet_rcv_has_room()
1280 int rcvbuf = READ_ONCE(sk->sk_rcvbuf); in __packet_rcv_has_room()
1281 int avail = rcvbuf - atomic_read(&sk->sk_rmem_alloc) in __packet_rcv_has_room()
1282 - (skb ? skb->truesize : 0); in __packet_rcv_has_room()
1292 if (po->tp_version == TPACKET_V3) { in __packet_rcv_has_room()
1314 if (READ_ONCE(po->pressure) != pressure) in packet_rcv_has_room()
1315 WRITE_ONCE(po->pressure, pressure); in packet_rcv_has_room()
1322 if (READ_ONCE(po->pressure) && in packet_rcv_try_clear_pressure()
1324 WRITE_ONCE(po->pressure, 0); in packet_rcv_try_clear_pressure()
1329 skb_queue_purge(&sk->sk_error_queue); in packet_sock_destruct()
1331 WARN_ON(atomic_read(&sk->sk_rmem_alloc)); in packet_sock_destruct()
1332 WARN_ON(refcount_read(&sk->sk_wmem_alloc)); in packet_sock_destruct()
1344 u32 *history = po->rollover->history; in fanout_flow_is_huge()
1373 unsigned int val = atomic_inc_return(&f->rr_cur); in fanout_demux_lb()
1400 po = pkt_sk(rcu_dereference(f->arr[idx])); in fanout_demux_rollover()
1410 i = j = min_t(int, po->rollover->sock, num - 1); in fanout_demux_rollover()
1412 po_next = pkt_sk(rcu_dereference(f->arr[i])); in fanout_demux_rollover()
1413 if (po_next != po_skip && !READ_ONCE(po_next->pressure) && in fanout_demux_rollover()
1416 po->rollover->sock = i; in fanout_demux_rollover()
1417 atomic_long_inc(&po->rollover->num); in fanout_demux_rollover()
1419 atomic_long_inc(&po->rollover->num_huge); in fanout_demux_rollover()
1427 atomic_long_inc(&po->rollover->num_failed); in fanout_demux_rollover()
1446 prog = rcu_dereference(f->bpf_prog); in fanout_demux_bpf()
1456 return f->flags & (flag >> 8); in fanout_has_flag()
1462 struct packet_fanout *f = pt->af_packet_priv; in packet_rcv_fanout()
1463 unsigned int num = READ_ONCE(f->num_members); in packet_rcv_fanout()
1464 struct net *net = read_pnet(&f->net); in packet_rcv_fanout()
1478 switch (f->type) { in packet_rcv_fanout()
1507 po = pkt_sk(rcu_dereference(f->arr[idx])); in packet_rcv_fanout()
1508 return po->prot_hook.func(skb, dev, &po->prot_hook, orig_dev); in packet_rcv_fanout()
1518 struct packet_fanout *f = po->fanout; in __fanout_link()
1520 spin_lock(&f->lock); in __fanout_link()
1521 rcu_assign_pointer(f->arr[f->num_members], sk); in __fanout_link()
1523 f->num_members++; in __fanout_link()
1524 if (f->num_members == 1) in __fanout_link()
1525 dev_add_pack(&f->prot_hook); in __fanout_link()
1526 spin_unlock(&f->lock); in __fanout_link()
1531 struct packet_fanout *f = po->fanout; in __fanout_unlink()
1534 spin_lock(&f->lock); in __fanout_unlink()
1535 for (i = 0; i < f->num_members; i++) { in __fanout_unlink()
1536 if (rcu_dereference_protected(f->arr[i], in __fanout_unlink()
1537 lockdep_is_held(&f->lock)) == sk) in __fanout_unlink()
1540 BUG_ON(i >= f->num_members); in __fanout_unlink()
1541 rcu_assign_pointer(f->arr[i], in __fanout_unlink()
1542 rcu_dereference_protected(f->arr[f->num_members - 1], in __fanout_unlink()
1543 lockdep_is_held(&f->lock))); in __fanout_unlink()
1544 f->num_members--; in __fanout_unlink()
1545 if (f->num_members == 0) in __fanout_unlink()
1546 __dev_remove_pack(&f->prot_hook); in __fanout_unlink()
1547 spin_unlock(&f->lock); in __fanout_unlink()
1552 if (sk->sk_family != PF_PACKET) in match_fanout_group()
1555 return ptype->af_packet_priv == pkt_sk(sk)->fanout; in match_fanout_group()
1560 switch (f->type) { in fanout_init_data()
1562 atomic_set(&f->rr_cur, 0); in fanout_init_data()
1566 RCU_INIT_POINTER(f->bpf_prog, NULL); in fanout_init_data()
1575 spin_lock(&f->lock); in __fanout_set_data_bpf()
1576 old = rcu_dereference_protected(f->bpf_prog, lockdep_is_held(&f->lock)); in __fanout_set_data_bpf()
1577 rcu_assign_pointer(f->bpf_prog, new); in __fanout_set_data_bpf()
1578 spin_unlock(&f->lock); in __fanout_set_data_bpf()
1593 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED)) in fanout_set_data_cbpf()
1594 return -EPERM; in fanout_set_data_cbpf()
1604 __fanout_set_data_bpf(po->fanout, new); in fanout_set_data_cbpf()
1614 if (sock_flag(&po->sk, SOCK_FILTER_LOCKED)) in fanout_set_data_ebpf()
1615 return -EPERM; in fanout_set_data_ebpf()
1617 return -EINVAL; in fanout_set_data_ebpf()
1619 return -EFAULT; in fanout_set_data_ebpf()
1625 __fanout_set_data_bpf(po->fanout, new); in fanout_set_data_ebpf()
1632 switch (po->fanout->type) { in fanout_set_data()
1638 return -EINVAL; in fanout_set_data()
1644 switch (f->type) { in fanout_release_data()
1656 if (f->id == candidate_id && in __fanout_id_is_free()
1657 read_pnet(&f->net) == sock_net(sk)) { in __fanout_id_is_free()
1685 u16 type_flags = args->type_flags; in fanout_add()
1689 u16 id = args->id; in fanout_add()
1695 return -EINVAL; in fanout_add()
1706 return -EINVAL; in fanout_add()
1711 err = -EALREADY; in fanout_add()
1712 if (po->fanout) in fanout_add()
1717 err = -ENOMEM; in fanout_add()
1721 atomic_long_set(&rollover->num, 0); in fanout_add()
1722 atomic_long_set(&rollover->num_huge, 0); in fanout_add()
1723 atomic_long_set(&rollover->num_failed, 0); in fanout_add()
1728 err = -EINVAL; in fanout_add()
1732 err = -ENOMEM; in fanout_add()
1741 if (f->id == id && in fanout_add()
1742 read_pnet(&f->net) == sock_net(sk)) { in fanout_add()
1747 err = -EINVAL; in fanout_add()
1749 if (match->flags != flags) in fanout_add()
1751 if (args->max_num_members && in fanout_add()
1752 args->max_num_members != match->max_num_members) in fanout_add()
1755 if (args->max_num_members > PACKET_FANOUT_MAX) in fanout_add()
1757 if (!args->max_num_members) in fanout_add()
1759 args->max_num_members = 256; in fanout_add()
1760 err = -ENOMEM; in fanout_add()
1761 match = kvzalloc(struct_size(match, arr, args->max_num_members), in fanout_add()
1765 write_pnet(&match->net, sock_net(sk)); in fanout_add()
1766 match->id = id; in fanout_add()
1767 match->type = type; in fanout_add()
1768 match->flags = flags; in fanout_add()
1769 INIT_LIST_HEAD(&match->list); in fanout_add()
1770 spin_lock_init(&match->lock); in fanout_add()
1771 refcount_set(&match->sk_ref, 0); in fanout_add()
1773 match->prot_hook.type = po->prot_hook.type; in fanout_add()
1774 match->prot_hook.dev = po->prot_hook.dev; in fanout_add()
1775 match->prot_hook.func = packet_rcv_fanout; in fanout_add()
1776 match->prot_hook.af_packet_priv = match; in fanout_add()
1777 match->prot_hook.af_packet_net = read_pnet(&match->net); in fanout_add()
1778 match->prot_hook.id_match = match_fanout_group; in fanout_add()
1779 match->max_num_members = args->max_num_members; in fanout_add()
1780 list_add(&match->list, &fanout_list); in fanout_add()
1782 err = -EINVAL; in fanout_add()
1784 spin_lock(&po->bind_lock); in fanout_add()
1785 if (po->running && in fanout_add()
1786 match->type == type && in fanout_add()
1787 match->prot_hook.type == po->prot_hook.type && in fanout_add()
1788 match->prot_hook.dev == po->prot_hook.dev) { in fanout_add()
1789 err = -ENOSPC; in fanout_add()
1790 if (refcount_read(&match->sk_ref) < match->max_num_members) { in fanout_add()
1791 __dev_remove_pack(&po->prot_hook); in fanout_add()
1794 WRITE_ONCE(po->fanout, match); in fanout_add()
1796 po->rollover = rollover; in fanout_add()
1798 refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1); in fanout_add()
1803 spin_unlock(&po->bind_lock); in fanout_add()
1805 if (err && !refcount_read(&match->sk_ref)) { in fanout_add()
1806 list_del(&match->list); in fanout_add()
1816 /* If pkt_sk(sk)->fanout->sk_ref is zero, this function removes
1817 * pkt_sk(sk)->fanout from fanout_list and returns pkt_sk(sk)->fanout.
1827 f = po->fanout; in fanout_release()
1829 po->fanout = NULL; in fanout_release()
1831 if (refcount_dec_and_test(&f->sk_ref)) in fanout_release()
1832 list_del(&f->list); in fanout_release()
1844 /* Earlier code assumed this would be a VLAN pkt, double-check in packet_extra_vlan_len_allowed()
1848 if (unlikely(dev->type != ARPHRD_ETHER)) in packet_extra_vlan_len_allowed()
1852 return likely(eth_hdr(skb)->h_proto == htons(ETH_P_8021Q)); in packet_extra_vlan_len_allowed()
1870 sk = pt->af_packet_priv; in packet_rcv_spkt()
1879 * For outgoing ones skb->data == skb_mac_header(skb) in packet_rcv_spkt()
1883 if (skb->pkt_type == PACKET_LOOPBACK) in packet_rcv_spkt()
1899 spkt = &PACKET_SKB_CB(skb)->sa.pkt; in packet_rcv_spkt()
1901 skb_push(skb, skb->data - skb_mac_header(skb)); in packet_rcv_spkt()
1907 spkt->spkt_family = dev->type; in packet_rcv_spkt()
1908 strscpy(spkt->spkt_device, dev->name, sizeof(spkt->spkt_device)); in packet_rcv_spkt()
1909 spkt->spkt_protocol = skb->protocol; in packet_rcv_spkt()
1929 if ((!skb->protocol || skb->protocol == htons(ETH_P_ALL)) && in packet_parse_headers()
1930 sock->type == SOCK_RAW) { in packet_parse_headers()
1932 skb->protocol = dev_parse_header_protocol(skb); in packet_parse_headers()
1936 if (likely(skb->dev->type == ARPHRD_ETHER) && in packet_parse_headers()
1937 eth_type_vlan(skb->protocol) && in packet_parse_headers()
1938 __vlan_get_protocol(skb, skb->protocol, &depth) != 0) { in packet_parse_headers()
1954 struct sock *sk = sock->sk; in packet_sendmsg_spkt()
1955 DECLARE_SOCKADDR(struct sockaddr_pkt *, saddr, msg->msg_name); in packet_sendmsg_spkt()
1968 if (msg->msg_namelen < sizeof(struct sockaddr)) in packet_sendmsg_spkt()
1969 return -EINVAL; in packet_sendmsg_spkt()
1970 if (msg->msg_namelen == sizeof(struct sockaddr_pkt)) in packet_sendmsg_spkt()
1971 proto = saddr->spkt_protocol; in packet_sendmsg_spkt()
1973 return -ENOTCONN; /* SOCK_PACKET must be sent giving an address */ in packet_sendmsg_spkt()
1979 saddr->spkt_device[sizeof(saddr->spkt_device) - 1] = 0; in packet_sendmsg_spkt()
1982 dev = dev_get_by_name_rcu(sock_net(sk), saddr->spkt_device); in packet_sendmsg_spkt()
1983 err = -ENODEV; in packet_sendmsg_spkt()
1987 err = -ENETDOWN; in packet_sendmsg_spkt()
1988 if (!(dev->flags & IFF_UP)) in packet_sendmsg_spkt()
1998 err = -EPROTONOSUPPORT; in packet_sendmsg_spkt()
2004 err = -EMSGSIZE; in packet_sendmsg_spkt()
2005 if (len > dev->mtu + dev->hard_header_len + VLAN_HLEN + extra_len) in packet_sendmsg_spkt()
2010 int tlen = dev->needed_tailroom; in packet_sendmsg_spkt()
2011 unsigned int hhlen = dev->header_ops ? dev->hard_header_len : 0; in packet_sendmsg_spkt()
2016 return -ENOBUFS; in packet_sendmsg_spkt()
2026 skb->data -= hhlen; in packet_sendmsg_spkt()
2027 skb->tail -= hhlen; in packet_sendmsg_spkt()
2037 if (!dev_validate_header(dev, skb->data, len)) { in packet_sendmsg_spkt()
2038 err = -EINVAL; in packet_sendmsg_spkt()
2041 if (len > (dev->mtu + dev->hard_header_len + extra_len) && in packet_sendmsg_spkt()
2043 err = -EMSGSIZE; in packet_sendmsg_spkt()
2048 if (msg->msg_controllen) { in packet_sendmsg_spkt()
2054 skb->protocol = proto; in packet_sendmsg_spkt()
2055 skb->dev = dev; in packet_sendmsg_spkt()
2056 skb->priority = sk->sk_priority; in packet_sendmsg_spkt()
2057 skb->mark = sk->sk_mark; in packet_sendmsg_spkt()
2058 skb->tstamp = sockc.transmit_time; in packet_sendmsg_spkt()
2063 skb->no_fcs = 1; in packet_sendmsg_spkt()
2085 filter = rcu_dereference(sk->sk_filter); in run_filter()
2087 res = bpf_prog_run_clear_cb(filter->prog, skb); in run_filter()
2099 return -EINVAL; in packet_rcv_vnet()
2100 *len -= sizeof(vnet_hdr); in packet_rcv_vnet()
2103 return -EINVAL; in packet_rcv_vnet()
2112 * Note tricky part: we DO mangle shared skb! skb->data, skb->len
2113 * and skb->cb are mangled. It works because (and until) packets
2126 u8 *skb_head = skb->data; in packet_rcv()
2127 int skb_len = skb->len; in packet_rcv()
2131 if (skb->pkt_type == PACKET_LOOPBACK) in packet_rcv()
2134 sk = pt->af_packet_priv; in packet_rcv()
2140 skb->dev = dev; in packet_rcv()
2150 if (sk->sk_type != SOCK_DGRAM) in packet_rcv()
2151 skb_push(skb, skb->data - skb_mac_header(skb)); in packet_rcv()
2152 else if (skb->pkt_type == PACKET_OUTGOING) { in packet_rcv()
2158 snaplen = skb->len; in packet_rcv()
2166 if (atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) in packet_rcv()
2174 if (skb_head != skb->data) { in packet_rcv()
2175 skb->data = skb_head; in packet_rcv()
2176 skb->len = skb_len; in packet_rcv()
2182 sock_skb_cb_check_size(sizeof(*PACKET_SKB_CB(skb)) + MAX_ADDR_LEN - 8); in packet_rcv()
2184 sll = &PACKET_SKB_CB(skb)->sa.ll; in packet_rcv()
2185 sll->sll_hatype = dev->type; in packet_rcv()
2186 sll->sll_pkttype = skb->pkt_type; in packet_rcv()
2187 if (unlikely(po->origdev)) in packet_rcv()
2188 sll->sll_ifindex = orig_dev->ifindex; in packet_rcv()
2190 sll->sll_ifindex = dev->ifindex; in packet_rcv()
2192 sll->sll_halen = dev_parse_header(skb, sll->sll_addr); in packet_rcv()
2194 /* sll->sll_family and sll->sll_protocol are set in packet_recvmsg(). in packet_rcv()
2197 PACKET_SKB_CB(skb)->sa.origlen = skb->len; in packet_rcv()
2203 skb->dev = NULL; in packet_rcv()
2209 spin_lock(&sk->sk_receive_queue.lock); in packet_rcv()
2210 po->stats.stats1.tp_packets++; in packet_rcv()
2213 __skb_queue_tail(&sk->sk_receive_queue, skb); in packet_rcv()
2214 spin_unlock(&sk->sk_receive_queue.lock); in packet_rcv()
2215 sk->sk_data_ready(sk); in packet_rcv()
2220 atomic_inc(&po->tp_drops); in packet_rcv()
2221 atomic_inc(&sk->sk_drops); in packet_rcv()
2224 if (skb_head != skb->data && skb_shared(skb)) { in packet_rcv()
2225 skb->data = skb_head; in packet_rcv()
2226 skb->len = skb_len; in packet_rcv()
2243 u8 *skb_head = skb->data; in tpacket_rcv()
2244 int skb_len = skb->len; in tpacket_rcv()
2263 if (skb->pkt_type == PACKET_LOOPBACK) in tpacket_rcv()
2266 sk = pt->af_packet_priv; in tpacket_rcv()
2273 if (sk->sk_type != SOCK_DGRAM) in tpacket_rcv()
2274 skb_push(skb, skb->data - skb_mac_header(skb)); in tpacket_rcv()
2275 else if (skb->pkt_type == PACKET_OUTGOING) { in tpacket_rcv()
2281 snaplen = skb->len; in tpacket_rcv()
2289 atomic_inc(&po->tp_drops); in tpacket_rcv()
2293 if (skb->ip_summed == CHECKSUM_PARTIAL) in tpacket_rcv()
2295 else if (skb->pkt_type != PACKET_OUTGOING && in tpacket_rcv()
2302 if (sk->sk_type == SOCK_DGRAM) { in tpacket_rcv()
2303 macoff = netoff = TPACKET_ALIGN(po->tp_hdrlen) + 16 + in tpacket_rcv()
2304 po->tp_reserve; in tpacket_rcv()
2307 netoff = TPACKET_ALIGN(po->tp_hdrlen + in tpacket_rcv()
2309 po->tp_reserve; in tpacket_rcv()
2310 if (po->has_vnet_hdr) { in tpacket_rcv()
2314 macoff = netoff - maclen; in tpacket_rcv()
2317 atomic_inc(&po->tp_drops); in tpacket_rcv()
2320 if (po->tp_version <= TPACKET_V2) { in tpacket_rcv()
2321 if (macoff + snaplen > po->rx_ring.frame_size) { in tpacket_rcv()
2322 if (po->copy_thresh && in tpacket_rcv()
2323 atomic_read(&sk->sk_rmem_alloc) < sk->sk_rcvbuf) { in tpacket_rcv()
2328 skb_head = skb->data; in tpacket_rcv()
2331 memset(&PACKET_SKB_CB(copy_skb)->sa.ll, 0, in tpacket_rcv()
2332 sizeof(PACKET_SKB_CB(copy_skb)->sa.ll)); in tpacket_rcv()
2336 snaplen = po->rx_ring.frame_size - macoff; in tpacket_rcv()
2343 GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len)) { in tpacket_rcv()
2346 nval = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len - macoff; in tpacket_rcv()
2352 macoff = GET_PBDQC_FROM_RB(&po->rx_ring)->max_frame_len; in tpacket_rcv()
2356 spin_lock(&sk->sk_receive_queue.lock); in tpacket_rcv()
2362 if (po->tp_version <= TPACKET_V2) { in tpacket_rcv()
2363 slot_id = po->rx_ring.head; in tpacket_rcv()
2364 if (test_bit(slot_id, po->rx_ring.rx_owner_map)) in tpacket_rcv()
2366 __set_bit(slot_id, po->rx_ring.rx_owner_map); in tpacket_rcv()
2370 virtio_net_hdr_from_skb(skb, h.raw + macoff - in tpacket_rcv()
2373 if (po->tp_version == TPACKET_V3) in tpacket_rcv()
2374 prb_clear_blk_fill_status(&po->rx_ring); in tpacket_rcv()
2378 if (po->tp_version <= TPACKET_V2) { in tpacket_rcv()
2379 packet_increment_rx_head(po, &po->rx_ring); in tpacket_rcv()
2382 * because it's COR - Clear On Read. in tpacket_rcv()
2386 if (atomic_read(&po->tp_drops)) in tpacket_rcv()
2390 po->stats.stats1.tp_packets++; in tpacket_rcv()
2394 __skb_queue_tail(&sk->sk_receive_queue, copy_skb); in tpacket_rcv()
2396 spin_unlock(&sk->sk_receive_queue.lock); in tpacket_rcv()
2404 po->tp_tstamp | SOF_TIMESTAMPING_SOFTWARE); in tpacket_rcv()
2410 switch (po->tp_version) { in tpacket_rcv()
2412 h.h1->tp_len = skb->len; in tpacket_rcv()
2413 h.h1->tp_snaplen = snaplen; in tpacket_rcv()
2414 h.h1->tp_mac = macoff; in tpacket_rcv()
2415 h.h1->tp_net = netoff; in tpacket_rcv()
2416 h.h1->tp_sec = ts.tv_sec; in tpacket_rcv()
2417 h.h1->tp_usec = ts.tv_nsec / NSEC_PER_USEC; in tpacket_rcv()
2421 h.h2->tp_len = skb->len; in tpacket_rcv()
2422 h.h2->tp_snaplen = snaplen; in tpacket_rcv()
2423 h.h2->tp_mac = macoff; in tpacket_rcv()
2424 h.h2->tp_net = netoff; in tpacket_rcv()
2425 h.h2->tp_sec = ts.tv_sec; in tpacket_rcv()
2426 h.h2->tp_nsec = ts.tv_nsec; in tpacket_rcv()
2428 h.h2->tp_vlan_tci = skb_vlan_tag_get(skb); in tpacket_rcv()
2429 h.h2->tp_vlan_tpid = ntohs(skb->vlan_proto); in tpacket_rcv()
2432 h.h2->tp_vlan_tci = 0; in tpacket_rcv()
2433 h.h2->tp_vlan_tpid = 0; in tpacket_rcv()
2435 memset(h.h2->tp_padding, 0, sizeof(h.h2->tp_padding)); in tpacket_rcv()
2442 h.h3->tp_status |= status; in tpacket_rcv()
2443 h.h3->tp_len = skb->len; in tpacket_rcv()
2444 h.h3->tp_snaplen = snaplen; in tpacket_rcv()
2445 h.h3->tp_mac = macoff; in tpacket_rcv()
2446 h.h3->tp_net = netoff; in tpacket_rcv()
2447 h.h3->tp_sec = ts.tv_sec; in tpacket_rcv()
2448 h.h3->tp_nsec = ts.tv_nsec; in tpacket_rcv()
2449 memset(h.h3->tp_padding, 0, sizeof(h.h3->tp_padding)); in tpacket_rcv()
2457 sll->sll_halen = dev_parse_header(skb, sll->sll_addr); in tpacket_rcv()
2458 sll->sll_family = AF_PACKET; in tpacket_rcv()
2459 sll->sll_hatype = dev->type; in tpacket_rcv()
2460 sll->sll_protocol = skb->protocol; in tpacket_rcv()
2461 sll->sll_pkttype = skb->pkt_type; in tpacket_rcv()
2462 if (unlikely(po->origdev)) in tpacket_rcv()
2463 sll->sll_ifindex = orig_dev->ifindex; in tpacket_rcv()
2465 sll->sll_ifindex = dev->ifindex; in tpacket_rcv()
2470 if (po->tp_version <= TPACKET_V2) { in tpacket_rcv()
2482 if (po->tp_version <= TPACKET_V2) { in tpacket_rcv()
2483 spin_lock(&sk->sk_receive_queue.lock); in tpacket_rcv()
2485 __clear_bit(slot_id, po->rx_ring.rx_owner_map); in tpacket_rcv()
2486 spin_unlock(&sk->sk_receive_queue.lock); in tpacket_rcv()
2487 sk->sk_data_ready(sk); in tpacket_rcv()
2488 } else if (po->tp_version == TPACKET_V3) { in tpacket_rcv()
2489 prb_clear_blk_fill_status(&po->rx_ring); in tpacket_rcv()
2493 if (skb_head != skb->data && skb_shared(skb)) { in tpacket_rcv()
2494 skb->data = skb_head; in tpacket_rcv()
2495 skb->len = skb_len; in tpacket_rcv()
2505 spin_unlock(&sk->sk_receive_queue.lock); in tpacket_rcv()
2506 atomic_inc(&po->tp_drops); in tpacket_rcv()
2509 sk->sk_data_ready(sk); in tpacket_rcv()
2516 struct packet_sock *po = pkt_sk(skb->sk); in tpacket_destruct_skb()
2518 if (likely(po->tx_ring.pg_vec)) { in tpacket_destruct_skb()
2523 packet_dec_pending(&po->tx_ring); in tpacket_destruct_skb()
2528 if (!packet_read_pending(&po->tx_ring)) in tpacket_destruct_skb()
2529 complete(&po->skb_completion); in tpacket_destruct_skb()
2537 if ((vnet_hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && in __packet_snd_vnet_parse()
2538 (__virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) + in __packet_snd_vnet_parse()
2539 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2 > in __packet_snd_vnet_parse()
2540 __virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len))) in __packet_snd_vnet_parse()
2541 vnet_hdr->hdr_len = __cpu_to_virtio16(vio_le(), in __packet_snd_vnet_parse()
2542 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_start) + in __packet_snd_vnet_parse()
2543 __virtio16_to_cpu(vio_le(), vnet_hdr->csum_offset) + 2); in __packet_snd_vnet_parse()
2545 if (__virtio16_to_cpu(vio_le(), vnet_hdr->hdr_len) > len) in __packet_snd_vnet_parse()
2546 return -EINVAL; in __packet_snd_vnet_parse()
2555 return -EINVAL; in packet_snd_vnet_parse()
2556 *len -= sizeof(*vnet_hdr); in packet_snd_vnet_parse()
2558 if (!copy_from_iter_full(vnet_hdr, sizeof(*vnet_hdr), &msg->msg_iter)) in packet_snd_vnet_parse()
2559 return -EFAULT; in packet_snd_vnet_parse()
2571 struct socket *sock = po->sk.sk_socket; in tpacket_fill_skb()
2577 skb->protocol = proto; in tpacket_fill_skb()
2578 skb->dev = dev; in tpacket_fill_skb()
2579 skb->priority = po->sk.sk_priority; in tpacket_fill_skb()
2580 skb->mark = po->sk.sk_mark; in tpacket_fill_skb()
2581 skb->tstamp = sockc->transmit_time; in tpacket_fill_skb()
2582 skb_setup_tx_timestamp(skb, sockc->tsflags); in tpacket_fill_skb()
2590 if (sock->type == SOCK_DGRAM) { in tpacket_fill_skb()
2594 return -EINVAL; in tpacket_fill_skb()
2598 skb_push(skb, dev->hard_header_len); in tpacket_fill_skb()
2599 skb_put(skb, copylen - dev->hard_header_len); in tpacket_fill_skb()
2603 if (!dev_validate_header(dev, skb->data, hdrlen)) in tpacket_fill_skb()
2604 return -EINVAL; in tpacket_fill_skb()
2607 to_write -= hdrlen; in tpacket_fill_skb()
2611 len_max = PAGE_SIZE - offset; in tpacket_fill_skb()
2614 skb->data_len = to_write; in tpacket_fill_skb()
2615 skb->len += to_write; in tpacket_fill_skb()
2616 skb->truesize += to_write; in tpacket_fill_skb()
2617 refcount_add(to_write, &po->sk.sk_wmem_alloc); in tpacket_fill_skb()
2620 nr_frags = skb_shinfo(skb)->nr_frags; in tpacket_fill_skb()
2625 return -EFAULT; in tpacket_fill_skb()
2633 to_write -= len; in tpacket_fill_skb()
2652 switch (po->tp_version) { in tpacket_parse_header()
2654 if (ph.h3->tp_next_offset != 0) { in tpacket_parse_header()
2656 return -EINVAL; in tpacket_parse_header()
2658 tp_len = ph.h3->tp_len; in tpacket_parse_header()
2661 tp_len = ph.h2->tp_len; in tpacket_parse_header()
2664 tp_len = ph.h1->tp_len; in tpacket_parse_header()
2669 return -EMSGSIZE; in tpacket_parse_header()
2672 if (unlikely(po->tp_tx_has_off)) { in tpacket_parse_header()
2675 off_min = po->tp_hdrlen - sizeof(struct sockaddr_ll); in tpacket_parse_header()
2676 off_max = po->tx_ring.frame_size - tp_len; in tpacket_parse_header()
2677 if (po->sk.sk_type == SOCK_DGRAM) { in tpacket_parse_header()
2678 switch (po->tp_version) { in tpacket_parse_header()
2680 off = ph.h3->tp_net; in tpacket_parse_header()
2683 off = ph.h2->tp_net; in tpacket_parse_header()
2686 off = ph.h1->tp_net; in tpacket_parse_header()
2690 switch (po->tp_version) { in tpacket_parse_header()
2692 off = ph.h3->tp_mac; in tpacket_parse_header()
2695 off = ph.h2->tp_mac; in tpacket_parse_header()
2698 off = ph.h1->tp_mac; in tpacket_parse_header()
2703 return -EINVAL; in tpacket_parse_header()
2705 off = po->tp_hdrlen - sizeof(struct sockaddr_ll); in tpacket_parse_header()
2721 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name); in tpacket_snd()
2722 bool need_wait = !(msg->msg_flags & MSG_DONTWAIT); in tpacket_snd()
2731 mutex_lock(&po->pg_vec_lock); in tpacket_snd()
2736 if (unlikely(!po->tx_ring.pg_vec)) { in tpacket_snd()
2737 err = -EBUSY; in tpacket_snd()
2742 proto = READ_ONCE(po->num); in tpacket_snd()
2744 err = -EINVAL; in tpacket_snd()
2745 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) in tpacket_snd()
2747 if (msg->msg_namelen < (saddr->sll_halen in tpacket_snd()
2751 proto = saddr->sll_protocol; in tpacket_snd()
2752 dev = dev_get_by_index(sock_net(&po->sk), saddr->sll_ifindex); in tpacket_snd()
2753 if (po->sk.sk_socket->type == SOCK_DGRAM) { in tpacket_snd()
2754 if (dev && msg->msg_namelen < dev->addr_len + in tpacket_snd()
2757 addr = saddr->sll_addr; in tpacket_snd()
2761 err = -ENXIO; in tpacket_snd()
2764 err = -ENETDOWN; in tpacket_snd()
2765 if (unlikely(!(dev->flags & IFF_UP))) in tpacket_snd()
2768 sockcm_init(&sockc, &po->sk); in tpacket_snd()
2769 if (msg->msg_controllen) { in tpacket_snd()
2770 err = sock_cmsg_send(&po->sk, msg, &sockc); in tpacket_snd()
2775 if (po->sk.sk_socket->type == SOCK_RAW) in tpacket_snd()
2776 reserve = dev->hard_header_len; in tpacket_snd()
2777 size_max = po->tx_ring.frame_size in tpacket_snd()
2778 - (po->tp_hdrlen - sizeof(struct sockaddr_ll)); in tpacket_snd()
2780 if ((size_max > dev->mtu + reserve + VLAN_HLEN) && !po->has_vnet_hdr) in tpacket_snd()
2781 size_max = dev->mtu + reserve + VLAN_HLEN; in tpacket_snd()
2783 reinit_completion(&po->skb_completion); in tpacket_snd()
2786 ph = packet_current_frame(po, &po->tx_ring, in tpacket_snd()
2790 timeo = sock_sndtimeo(&po->sk, msg->msg_flags & MSG_DONTWAIT); in tpacket_snd()
2791 timeo = wait_for_completion_interruptible_timeout(&po->skb_completion, timeo); in tpacket_snd()
2793 err = !timeo ? -ETIMEDOUT : -ERESTARTSYS; in tpacket_snd()
2808 tlen = dev->needed_tailroom; in tpacket_snd()
2809 if (po->has_vnet_hdr) { in tpacket_snd()
2812 tp_len -= sizeof(*vnet_hdr); in tpacket_snd()
2815 tp_len = -EINVAL; in tpacket_snd()
2819 vnet_hdr->hdr_len); in tpacket_snd()
2821 copylen = max_t(int, copylen, dev->hard_header_len); in tpacket_snd()
2822 skb = sock_alloc_send_skb(&po->sk, in tpacket_snd()
2824 (copylen - dev->hard_header_len), in tpacket_snd()
2836 tp_len > dev->mtu + reserve && in tpacket_snd()
2837 !po->has_vnet_hdr && in tpacket_snd()
2839 tp_len = -EMSGSIZE; in tpacket_snd()
2843 if (po->tp_loss) { in tpacket_snd()
2846 packet_increment_head(&po->tx_ring); in tpacket_snd()
2856 if (po->has_vnet_hdr) { in tpacket_snd()
2858 tp_len = -EINVAL; in tpacket_snd()
2864 skb->destructor = tpacket_destruct_skb; in tpacket_snd()
2866 packet_inc_pending(&po->tx_ring); in tpacket_snd()
2869 err = po->xmit(skb); in tpacket_snd()
2885 packet_increment_head(&po->tx_ring); in tpacket_snd()
2889 * to call it as it's per_cpu variable, but in fast-path in tpacket_snd()
2890 * we already short-circuit the loop with the first in tpacket_snd()
2894 (need_wait && packet_read_pending(&po->tx_ring)))); in tpacket_snd()
2905 mutex_unlock(&po->pg_vec_lock); in tpacket_snd()
2920 skb = sock_alloc_send_pskb(sk, prepad + linear, len - linear, noblock, in packet_alloc_skb()
2927 skb->data_len = len - linear; in packet_alloc_skb()
2928 skb->len += len - linear; in packet_alloc_skb()
2935 struct sock *sk = sock->sk; in packet_snd()
2936 DECLARE_SOCKADDR(struct sockaddr_ll *, saddr, msg->msg_name); in packet_snd()
2956 proto = READ_ONCE(po->num); in packet_snd()
2958 err = -EINVAL; in packet_snd()
2959 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) in packet_snd()
2961 if (msg->msg_namelen < (saddr->sll_halen + offsetof(struct sockaddr_ll, sll_addr))) in packet_snd()
2963 proto = saddr->sll_protocol; in packet_snd()
2964 dev = dev_get_by_index(sock_net(sk), saddr->sll_ifindex); in packet_snd()
2965 if (sock->type == SOCK_DGRAM) { in packet_snd()
2966 if (dev && msg->msg_namelen < dev->addr_len + in packet_snd()
2969 addr = saddr->sll_addr; in packet_snd()
2973 err = -ENXIO; in packet_snd()
2976 err = -ENETDOWN; in packet_snd()
2977 if (unlikely(!(dev->flags & IFF_UP))) in packet_snd()
2981 sockc.mark = sk->sk_mark; in packet_snd()
2982 if (msg->msg_controllen) { in packet_snd()
2988 if (sock->type == SOCK_RAW) in packet_snd()
2989 reserve = dev->hard_header_len; in packet_snd()
2990 if (po->has_vnet_hdr) { in packet_snd()
2999 err = -EPROTONOSUPPORT; in packet_snd()
3005 err = -EMSGSIZE; in packet_snd()
3007 (len > dev->mtu + reserve + VLAN_HLEN + extra_len)) in packet_snd()
3010 err = -ENOBUFS; in packet_snd()
3012 tlen = dev->needed_tailroom; in packet_snd()
3014 linear = max(linear, min_t(int, len, dev->hard_header_len)); in packet_snd()
3016 msg->msg_flags & MSG_DONTWAIT, &err); in packet_snd()
3022 err = -EINVAL; in packet_snd()
3023 if (sock->type == SOCK_DGRAM) { in packet_snd()
3028 skb_reserve(skb, -reserve); in packet_snd()
3030 dev->min_header_len != dev->hard_header_len) in packet_snd()
3034 /* Returns -EFAULT on error */ in packet_snd()
3035 err = skb_copy_datagram_from_iter(skb, offset, &msg->msg_iter, len); in packet_snd()
3039 if ((sock->type == SOCK_RAW && in packet_snd()
3040 !dev_validate_header(dev, skb->data, len)) || !skb->len) { in packet_snd()
3041 err = -EINVAL; in packet_snd()
3047 if (!vnet_hdr.gso_type && (len > dev->mtu + reserve + extra_len) && in packet_snd()
3049 err = -EMSGSIZE; in packet_snd()
3053 skb->protocol = proto; in packet_snd()
3054 skb->dev = dev; in packet_snd()
3055 skb->priority = sk->sk_priority; in packet_snd()
3056 skb->mark = sockc.mark; in packet_snd()
3057 skb->tstamp = sockc.transmit_time; in packet_snd()
3060 skb->no_fcs = 1; in packet_snd()
3072 err = po->xmit(skb); in packet_snd()
3094 struct sock *sk = sock->sk; in packet_sendmsg()
3100 if (data_race(po->tx_ring.pg_vec)) in packet_sendmsg()
3113 struct sock *sk = sock->sk; in packet_release()
3125 mutex_lock(&net->packet.sklist_lock); in packet_release()
3127 mutex_unlock(&net->packet.sklist_lock); in packet_release()
3129 sock_prot_inuse_add(net, sk->sk_prot, -1); in packet_release()
3131 spin_lock(&po->bind_lock); in packet_release()
3135 if (po->prot_hook.dev) { in packet_release()
3136 netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker); in packet_release()
3137 po->prot_hook.dev = NULL; in packet_release()
3139 spin_unlock(&po->bind_lock); in packet_release()
3144 if (po->rx_ring.pg_vec) { in packet_release()
3149 if (po->tx_ring.pg_vec) { in packet_release()
3159 kfree(po->rollover); in packet_release()
3168 sock->sk = NULL; in packet_release()
3172 skb_queue_purge(&sk->sk_receive_queue); in packet_release()
3194 spin_lock(&po->bind_lock); in packet_do_bind()
3197 if (po->fanout) { in packet_do_bind()
3198 ret = -EINVAL; in packet_do_bind()
3205 ret = -ENODEV; in packet_do_bind()
3211 ret = -ENODEV; in packet_do_bind()
3216 need_rehook = po->prot_hook.type != proto || po->prot_hook.dev != dev; in packet_do_bind()
3220 if (po->running) { in packet_do_bind()
3225 WRITE_ONCE(po->num, 0); in packet_do_bind()
3230 dev->ifindex); in packet_do_bind()
3233 BUG_ON(po->running); in packet_do_bind()
3234 WRITE_ONCE(po->num, proto); in packet_do_bind()
3235 po->prot_hook.type = proto; in packet_do_bind()
3237 netdev_put(po->prot_hook.dev, &po->prot_hook.dev_tracker); in packet_do_bind()
3240 po->prot_hook.dev = NULL; in packet_do_bind()
3241 WRITE_ONCE(po->ifindex, -1); in packet_do_bind()
3244 netdev_hold(dev, &po->prot_hook.dev_tracker, in packet_do_bind()
3246 po->prot_hook.dev = dev; in packet_do_bind()
3247 WRITE_ONCE(po->ifindex, dev ? dev->ifindex : 0); in packet_do_bind()
3256 if (!unlisted && (!dev || (dev->flags & IFF_UP))) { in packet_do_bind()
3259 sk->sk_err = ENETDOWN; in packet_do_bind()
3266 spin_unlock(&po->bind_lock); in packet_do_bind()
3278 struct sock *sk = sock->sk; in packet_bind_spkt()
3279 char name[sizeof(uaddr->sa_data) + 1]; in packet_bind_spkt()
3286 return -EINVAL; in packet_bind_spkt()
3287 /* uaddr->sa_data comes from the userspace, it's not guaranteed to be in packet_bind_spkt()
3288 * zero-terminated. in packet_bind_spkt()
3290 memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data)); in packet_bind_spkt()
3291 name[sizeof(uaddr->sa_data)] = 0; in packet_bind_spkt()
3293 return packet_do_bind(sk, name, 0, pkt_sk(sk)->num); in packet_bind_spkt()
3299 struct sock *sk = sock->sk; in packet_bind()
3306 return -EINVAL; in packet_bind()
3307 if (sll->sll_family != AF_PACKET) in packet_bind()
3308 return -EINVAL; in packet_bind()
3310 return packet_do_bind(sk, NULL, sll->sll_ifindex, in packet_bind()
3311 sll->sll_protocol ? : pkt_sk(sk)->num); in packet_bind()
3332 if (!ns_capable(net->user_ns, CAP_NET_RAW)) in packet_create()
3333 return -EPERM; in packet_create()
3334 if (sock->type != SOCK_DGRAM && sock->type != SOCK_RAW && in packet_create()
3335 sock->type != SOCK_PACKET) in packet_create()
3336 return -ESOCKTNOSUPPORT; in packet_create()
3338 sock->state = SS_UNCONNECTED; in packet_create()
3340 err = -ENOBUFS; in packet_create()
3345 sock->ops = &packet_ops; in packet_create()
3346 if (sock->type == SOCK_PACKET) in packet_create()
3347 sock->ops = &packet_ops_spkt; in packet_create()
3352 init_completion(&po->skb_completion); in packet_create()
3353 sk->sk_family = PF_PACKET; in packet_create()
3354 po->num = proto; in packet_create()
3355 po->xmit = dev_queue_xmit; in packet_create()
3363 sk->sk_destruct = packet_sock_destruct; in packet_create()
3370 spin_lock_init(&po->bind_lock); in packet_create()
3371 mutex_init(&po->pg_vec_lock); in packet_create()
3372 po->rollover = NULL; in packet_create()
3373 po->prot_hook.func = packet_rcv; in packet_create()
3375 if (sock->type == SOCK_PACKET) in packet_create()
3376 po->prot_hook.func = packet_rcv_spkt; in packet_create()
3378 po->prot_hook.af_packet_priv = sk; in packet_create()
3379 po->prot_hook.af_packet_net = sock_net(sk); in packet_create()
3382 po->prot_hook.type = proto; in packet_create()
3386 mutex_lock(&net->packet.sklist_lock); in packet_create()
3387 sk_add_node_tail_rcu(sk, &net->packet.sklist); in packet_create()
3388 mutex_unlock(&net->packet.sklist_lock); in packet_create()
3407 struct sock *sk = sock->sk; in packet_recvmsg()
3413 err = -EINVAL; in packet_recvmsg()
3419 if (pkt_sk(sk)->ifindex < 0) in packet_recvmsg()
3420 return -ENODEV; in packet_recvmsg()
3431 * of horrible races and re-entrancy so we can forget about it in packet_recvmsg()
3451 if (pkt_sk(sk)->has_vnet_hdr) { in packet_recvmsg()
3462 copied = skb->len; in packet_recvmsg()
3465 msg->msg_flags |= MSG_TRUNC; in packet_recvmsg()
3472 if (sock->type != SOCK_PACKET) { in packet_recvmsg()
3473 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll; in packet_recvmsg()
3476 origlen = PACKET_SKB_CB(skb)->sa.origlen; in packet_recvmsg()
3477 sll->sll_family = AF_PACKET; in packet_recvmsg()
3478 sll->sll_protocol = skb->protocol; in packet_recvmsg()
3483 if (msg->msg_name) { in packet_recvmsg()
3484 const size_t max_len = min(sizeof(skb->cb), in packet_recvmsg()
3491 if (sock->type == SOCK_PACKET) { in packet_recvmsg()
3493 msg->msg_namelen = sizeof(struct sockaddr_pkt); in packet_recvmsg()
3494 copy_len = msg->msg_namelen; in packet_recvmsg()
3496 struct sockaddr_ll *sll = &PACKET_SKB_CB(skb)->sa.ll; in packet_recvmsg()
3498 msg->msg_namelen = sll->sll_halen + in packet_recvmsg()
3500 copy_len = msg->msg_namelen; in packet_recvmsg()
3501 if (msg->msg_namelen < sizeof(struct sockaddr_ll)) { in packet_recvmsg()
3502 memset(msg->msg_name + in packet_recvmsg()
3504 0, sizeof(sll->sll_addr)); in packet_recvmsg()
3505 msg->msg_namelen = sizeof(struct sockaddr_ll); in packet_recvmsg()
3510 msg->msg_namelen = copy_len; in packet_recvmsg()
3512 memcpy(msg->msg_name, &PACKET_SKB_CB(skb)->sa, copy_len); in packet_recvmsg()
3515 if (pkt_sk(sk)->auxdata) { in packet_recvmsg()
3519 if (skb->ip_summed == CHECKSUM_PARTIAL) in packet_recvmsg()
3521 else if (skb->pkt_type != PACKET_OUTGOING && in packet_recvmsg()
3526 aux.tp_snaplen = skb->len; in packet_recvmsg()
3531 aux.tp_vlan_tpid = ntohs(skb->vlan_proto); in packet_recvmsg()
3542 * hides all the races and re-entrancy issues from us. in packet_recvmsg()
3544 err = vnet_hdr_len + ((flags&MSG_TRUNC) ? skb->len : copied); in packet_recvmsg()
3556 struct sock *sk = sock->sk; in packet_getname_spkt()
3559 return -EOPNOTSUPP; in packet_getname_spkt()
3561 uaddr->sa_family = AF_PACKET; in packet_getname_spkt()
3562 memset(uaddr->sa_data, 0, sizeof(uaddr->sa_data)); in packet_getname_spkt()
3564 dev = dev_get_by_index_rcu(sock_net(sk), READ_ONCE(pkt_sk(sk)->ifindex)); in packet_getname_spkt()
3566 strscpy(uaddr->sa_data, dev->name, sizeof(uaddr->sa_data)); in packet_getname_spkt()
3576 struct sock *sk = sock->sk; in packet_getname()
3582 return -EOPNOTSUPP; in packet_getname()
3584 ifindex = READ_ONCE(po->ifindex); in packet_getname()
3585 sll->sll_family = AF_PACKET; in packet_getname()
3586 sll->sll_ifindex = ifindex; in packet_getname()
3587 sll->sll_protocol = READ_ONCE(po->num); in packet_getname()
3588 sll->sll_pkttype = 0; in packet_getname()
3592 sll->sll_hatype = dev->type; in packet_getname()
3593 sll->sll_halen = dev->addr_len; in packet_getname()
3594 memcpy(sll->sll_addr, dev->dev_addr, dev->addr_len); in packet_getname()
3596 sll->sll_hatype = 0; /* Bad: we have no ARPHRD_UNSPEC */ in packet_getname()
3597 sll->sll_halen = 0; in packet_getname()
3601 return offsetof(struct sockaddr_ll, sll_addr) + sll->sll_halen; in packet_getname()
3607 switch (i->type) { in packet_dev_mc()
3609 if (i->alen != dev->addr_len) in packet_dev_mc()
3610 return -EINVAL; in packet_dev_mc()
3612 return dev_mc_add(dev, i->addr); in packet_dev_mc()
3614 return dev_mc_del(dev, i->addr); in packet_dev_mc()
3621 if (i->alen != dev->addr_len) in packet_dev_mc()
3622 return -EINVAL; in packet_dev_mc()
3624 return dev_uc_add(dev, i->addr); in packet_dev_mc()
3626 return dev_uc_del(dev, i->addr); in packet_dev_mc()
3640 if (ml->ifindex == dev->ifindex) { in packet_dev_mclist_delete()
3641 packet_dev_mc(dev, ml, -1); in packet_dev_mclist_delete()
3642 *mlp = ml->next; in packet_dev_mclist_delete()
3645 mlp = &ml->next; in packet_dev_mclist_delete()
3658 err = -ENODEV; in packet_mc_add()
3659 dev = __dev_get_by_index(sock_net(sk), mreq->mr_ifindex); in packet_mc_add()
3663 err = -EINVAL; in packet_mc_add()
3664 if (mreq->mr_alen > dev->addr_len) in packet_mc_add()
3667 err = -ENOBUFS; in packet_mc_add()
3673 for (ml = po->mclist; ml; ml = ml->next) { in packet_mc_add()
3674 if (ml->ifindex == mreq->mr_ifindex && in packet_mc_add()
3675 ml->type == mreq->mr_type && in packet_mc_add()
3676 ml->alen == mreq->mr_alen && in packet_mc_add()
3677 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { in packet_mc_add()
3678 ml->count++; in packet_mc_add()
3685 i->type = mreq->mr_type; in packet_mc_add()
3686 i->ifindex = mreq->mr_ifindex; in packet_mc_add()
3687 i->alen = mreq->mr_alen; in packet_mc_add()
3688 memcpy(i->addr, mreq->mr_address, i->alen); in packet_mc_add()
3689 memset(i->addr + i->alen, 0, sizeof(i->addr) - i->alen); in packet_mc_add()
3690 i->count = 1; in packet_mc_add()
3691 i->next = po->mclist; in packet_mc_add()
3692 po->mclist = i; in packet_mc_add()
3695 po->mclist = i->next; in packet_mc_add()
3710 for (mlp = &pkt_sk(sk)->mclist; (ml = *mlp) != NULL; mlp = &ml->next) { in packet_mc_drop()
3711 if (ml->ifindex == mreq->mr_ifindex && in packet_mc_drop()
3712 ml->type == mreq->mr_type && in packet_mc_drop()
3713 ml->alen == mreq->mr_alen && in packet_mc_drop()
3714 memcmp(ml->addr, mreq->mr_address, ml->alen) == 0) { in packet_mc_drop()
3715 if (--ml->count == 0) { in packet_mc_drop()
3717 *mlp = ml->next; in packet_mc_drop()
3718 dev = __dev_get_by_index(sock_net(sk), ml->ifindex); in packet_mc_drop()
3720 packet_dev_mc(dev, ml, -1); in packet_mc_drop()
3735 if (!po->mclist) in packet_flush_mclist()
3739 while ((ml = po->mclist) != NULL) { in packet_flush_mclist()
3742 po->mclist = ml->next; in packet_flush_mclist()
3743 dev = __dev_get_by_index(sock_net(sk), ml->ifindex); in packet_flush_mclist()
3745 packet_dev_mc(dev, ml, -1); in packet_flush_mclist()
3755 struct sock *sk = sock->sk; in packet_setsockopt()
3760 return -ENOPROTOOPT; in packet_setsockopt()
3770 return -EINVAL; in packet_setsockopt()
3774 return -EFAULT; in packet_setsockopt()
3776 return -EINVAL; in packet_setsockopt()
3791 switch (po->tp_version) { in packet_setsockopt()
3802 ret = -EINVAL; in packet_setsockopt()
3805 ret = -EFAULT; in packet_setsockopt()
3818 return -EINVAL; in packet_setsockopt()
3820 return -EFAULT; in packet_setsockopt()
3822 pkt_sk(sk)->copy_thresh = val; in packet_setsockopt()
3830 return -EINVAL; in packet_setsockopt()
3832 return -EFAULT; in packet_setsockopt()
3839 return -EINVAL; in packet_setsockopt()
3842 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { in packet_setsockopt()
3843 ret = -EBUSY; in packet_setsockopt()
3845 po->tp_version = val; in packet_setsockopt()
3856 return -EINVAL; in packet_setsockopt()
3858 return -EFAULT; in packet_setsockopt()
3860 return -EINVAL; in packet_setsockopt()
3862 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { in packet_setsockopt()
3863 ret = -EBUSY; in packet_setsockopt()
3865 po->tp_reserve = val; in packet_setsockopt()
3876 return -EINVAL; in packet_setsockopt()
3878 return -EFAULT; in packet_setsockopt()
3881 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { in packet_setsockopt()
3882 ret = -EBUSY; in packet_setsockopt()
3884 po->tp_loss = !!val; in packet_setsockopt()
3895 return -EINVAL; in packet_setsockopt()
3897 return -EFAULT; in packet_setsockopt()
3900 po->auxdata = !!val; in packet_setsockopt()
3909 return -EINVAL; in packet_setsockopt()
3911 return -EFAULT; in packet_setsockopt()
3914 po->origdev = !!val; in packet_setsockopt()
3922 if (sock->type != SOCK_RAW) in packet_setsockopt()
3923 return -EINVAL; in packet_setsockopt()
3925 return -EINVAL; in packet_setsockopt()
3927 return -EFAULT; in packet_setsockopt()
3930 if (po->rx_ring.pg_vec || po->tx_ring.pg_vec) { in packet_setsockopt()
3931 ret = -EBUSY; in packet_setsockopt()
3933 po->has_vnet_hdr = !!val; in packet_setsockopt()
3944 return -EINVAL; in packet_setsockopt()
3946 return -EFAULT; in packet_setsockopt()
3948 po->tp_tstamp = val; in packet_setsockopt()
3956 return -EINVAL; in packet_setsockopt()
3958 return -EFAULT; in packet_setsockopt()
3965 if (!READ_ONCE(po->fanout)) in packet_setsockopt()
3966 return -EINVAL; in packet_setsockopt()
3975 return -EINVAL; in packet_setsockopt()
3977 return -EFAULT; in packet_setsockopt()
3979 return -EINVAL; in packet_setsockopt()
3981 po->prot_hook.ignore_outgoing = !!val; in packet_setsockopt()
3989 return -EINVAL; in packet_setsockopt()
3991 return -EFAULT; in packet_setsockopt()
3994 if (!po->rx_ring.pg_vec && !po->tx_ring.pg_vec) in packet_setsockopt()
3995 po->tp_tx_has_off = !!val; in packet_setsockopt()
4005 return -EINVAL; in packet_setsockopt()
4007 return -EFAULT; in packet_setsockopt()
4009 po->xmit = val ? packet_direct_xmit : dev_queue_xmit; in packet_setsockopt()
4013 return -ENOPROTOOPT; in packet_setsockopt()
4022 struct sock *sk = sock->sk; in packet_getsockopt()
4030 return -ENOPROTOOPT; in packet_getsockopt()
4033 return -EFAULT; in packet_getsockopt()
4036 return -EINVAL; in packet_getsockopt()
4040 spin_lock_bh(&sk->sk_receive_queue.lock); in packet_getsockopt()
4041 memcpy(&st, &po->stats, sizeof(st)); in packet_getsockopt()
4042 memset(&po->stats, 0, sizeof(po->stats)); in packet_getsockopt()
4043 spin_unlock_bh(&sk->sk_receive_queue.lock); in packet_getsockopt()
4044 drops = atomic_xchg(&po->tp_drops, 0); in packet_getsockopt()
4046 if (po->tp_version == TPACKET_V3) { in packet_getsockopt()
4060 val = po->auxdata; in packet_getsockopt()
4063 val = po->origdev; in packet_getsockopt()
4066 val = po->has_vnet_hdr; in packet_getsockopt()
4069 val = po->tp_version; in packet_getsockopt()
4075 return -EINVAL; in packet_getsockopt()
4077 return -EFAULT; in packet_getsockopt()
4089 return -EINVAL; in packet_getsockopt()
4093 val = po->tp_reserve; in packet_getsockopt()
4096 val = po->tp_loss; in packet_getsockopt()
4099 val = po->tp_tstamp; in packet_getsockopt()
4102 val = (po->fanout ? in packet_getsockopt()
4103 ((u32)po->fanout->id | in packet_getsockopt()
4104 ((u32)po->fanout->type << 16) | in packet_getsockopt()
4105 ((u32)po->fanout->flags << 24)) : in packet_getsockopt()
4109 val = po->prot_hook.ignore_outgoing; in packet_getsockopt()
4112 if (!po->rollover) in packet_getsockopt()
4113 return -EINVAL; in packet_getsockopt()
4114 rstats.tp_all = atomic_long_read(&po->rollover->num); in packet_getsockopt()
4115 rstats.tp_huge = atomic_long_read(&po->rollover->num_huge); in packet_getsockopt()
4116 rstats.tp_failed = atomic_long_read(&po->rollover->num_failed); in packet_getsockopt()
4121 val = po->tp_tx_has_off; in packet_getsockopt()
4127 return -ENOPROTOOPT; in packet_getsockopt()
4133 return -EFAULT; in packet_getsockopt()
4135 return -EFAULT; in packet_getsockopt()
4147 sk_for_each_rcu(sk, &net->packet.sklist) { in packet_notifier()
4152 if (po->mclist) in packet_notifier()
4153 packet_dev_mclist_delete(dev, &po->mclist); in packet_notifier()
4157 if (dev->ifindex == po->ifindex) { in packet_notifier()
4158 spin_lock(&po->bind_lock); in packet_notifier()
4159 if (po->running) { in packet_notifier()
4161 sk->sk_err = ENETDOWN; in packet_notifier()
4167 WRITE_ONCE(po->ifindex, -1); in packet_notifier()
4168 netdev_put(po->prot_hook.dev, in packet_notifier()
4169 &po->prot_hook.dev_tracker); in packet_notifier()
4170 po->prot_hook.dev = NULL; in packet_notifier()
4172 spin_unlock(&po->bind_lock); in packet_notifier()
4176 if (dev->ifindex == po->ifindex) { in packet_notifier()
4177 spin_lock(&po->bind_lock); in packet_notifier()
4178 if (po->num) in packet_notifier()
4180 spin_unlock(&po->bind_lock); in packet_notifier()
4193 struct sock *sk = sock->sk; in packet_ioctl()
4207 spin_lock_bh(&sk->sk_receive_queue.lock); in packet_ioctl()
4208 skb = skb_peek(&sk->sk_receive_queue); in packet_ioctl()
4210 amount = skb->len; in packet_ioctl()
4211 spin_unlock_bh(&sk->sk_receive_queue.lock); in packet_ioctl()
4233 return -ENOIOCTLCMD; in packet_ioctl()
4241 struct sock *sk = sock->sk; in packet_poll()
4245 spin_lock_bh(&sk->sk_receive_queue.lock); in packet_poll()
4246 if (po->rx_ring.pg_vec) { in packet_poll()
4247 if (!packet_previous_rx_frame(po, &po->rx_ring, in packet_poll()
4252 spin_unlock_bh(&sk->sk_receive_queue.lock); in packet_poll()
4253 spin_lock_bh(&sk->sk_write_queue.lock); in packet_poll()
4254 if (po->tx_ring.pg_vec) { in packet_poll()
4255 if (packet_current_frame(po, &po->tx_ring, TP_STATUS_AVAILABLE)) in packet_poll()
4258 spin_unlock_bh(&sk->sk_write_queue.lock); in packet_poll()
4269 struct file *file = vma->vm_file; in packet_mm_open()
4270 struct socket *sock = file->private_data; in packet_mm_open()
4271 struct sock *sk = sock->sk; in packet_mm_open()
4274 atomic_inc(&pkt_sk(sk)->mapped); in packet_mm_open()
4279 struct file *file = vma->vm_file; in packet_mm_close()
4280 struct socket *sock = file->private_data; in packet_mm_close()
4281 struct sock *sk = sock->sk; in packet_mm_close()
4284 atomic_dec(&pkt_sk(sk)->mapped); in packet_mm_close()
4325 /* vmalloc failed, lets dig into swap here */ in alloc_one_pg_vec_page()
4337 unsigned int block_nr = req->tp_block_nr; in alloc_pg_vec()
4372 struct tpacket_req *req = &req_u->req; in packet_set_ring()
4374 rb = tx_ring ? &po->tx_ring : &po->rx_ring; in packet_set_ring()
4375 rb_queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; in packet_set_ring()
4377 err = -EBUSY; in packet_set_ring()
4379 if (atomic_read(&po->mapped)) in packet_set_ring()
4385 if (req->tp_block_nr) { in packet_set_ring()
4389 err = -EBUSY; in packet_set_ring()
4390 if (unlikely(rb->pg_vec)) in packet_set_ring()
4393 switch (po->tp_version) { in packet_set_ring()
4395 po->tp_hdrlen = TPACKET_HDRLEN; in packet_set_ring()
4398 po->tp_hdrlen = TPACKET2_HDRLEN; in packet_set_ring()
4401 po->tp_hdrlen = TPACKET3_HDRLEN; in packet_set_ring()
4405 err = -EINVAL; in packet_set_ring()
4406 if (unlikely((int)req->tp_block_size <= 0)) in packet_set_ring()
4408 if (unlikely(!PAGE_ALIGNED(req->tp_block_size))) in packet_set_ring()
4410 min_frame_size = po->tp_hdrlen + po->tp_reserve; in packet_set_ring()
4411 if (po->tp_version >= TPACKET_V3 && in packet_set_ring()
4412 req->tp_block_size < in packet_set_ring()
4413 BLK_PLUS_PRIV((u64)req_u->req3.tp_sizeof_priv) + min_frame_size) in packet_set_ring()
4415 if (unlikely(req->tp_frame_size < min_frame_size)) in packet_set_ring()
4417 if (unlikely(req->tp_frame_size & (TPACKET_ALIGNMENT - 1))) in packet_set_ring()
4420 rb->frames_per_block = req->tp_block_size / req->tp_frame_size; in packet_set_ring()
4421 if (unlikely(rb->frames_per_block == 0)) in packet_set_ring()
4423 if (unlikely(rb->frames_per_block > UINT_MAX / req->tp_block_nr)) in packet_set_ring()
4425 if (unlikely((rb->frames_per_block * req->tp_block_nr) != in packet_set_ring()
4426 req->tp_frame_nr)) in packet_set_ring()
4429 err = -ENOMEM; in packet_set_ring()
4430 order = get_order(req->tp_block_size); in packet_set_ring()
4434 switch (po->tp_version) { in packet_set_ring()
4440 struct tpacket_req3 *req3 = &req_u->req3; in packet_set_ring()
4442 if (req3->tp_retire_blk_tov || in packet_set_ring()
4443 req3->tp_sizeof_priv || in packet_set_ring()
4444 req3->tp_feature_req_word) { in packet_set_ring()
4445 err = -EINVAL; in packet_set_ring()
4452 rx_owner_map = bitmap_alloc(req->tp_frame_nr, in packet_set_ring()
4462 err = -EINVAL; in packet_set_ring()
4463 if (unlikely(req->tp_frame_nr)) in packet_set_ring()
4469 spin_lock(&po->bind_lock); in packet_set_ring()
4470 was_running = po->running; in packet_set_ring()
4471 num = po->num; in packet_set_ring()
4473 WRITE_ONCE(po->num, 0); in packet_set_ring()
4476 spin_unlock(&po->bind_lock); in packet_set_ring()
4480 err = -EBUSY; in packet_set_ring()
4481 mutex_lock(&po->pg_vec_lock); in packet_set_ring()
4482 if (closing || atomic_read(&po->mapped) == 0) { in packet_set_ring()
4484 spin_lock_bh(&rb_queue->lock); in packet_set_ring()
4485 swap(rb->pg_vec, pg_vec); in packet_set_ring()
4486 if (po->tp_version <= TPACKET_V2) in packet_set_ring()
4487 swap(rb->rx_owner_map, rx_owner_map); in packet_set_ring()
4488 rb->frame_max = (req->tp_frame_nr - 1); in packet_set_ring()
4489 rb->head = 0; in packet_set_ring()
4490 rb->frame_size = req->tp_frame_size; in packet_set_ring()
4491 spin_unlock_bh(&rb_queue->lock); in packet_set_ring()
4493 swap(rb->pg_vec_order, order); in packet_set_ring()
4494 swap(rb->pg_vec_len, req->tp_block_nr); in packet_set_ring()
4496 rb->pg_vec_pages = req->tp_block_size/PAGE_SIZE; in packet_set_ring()
4497 po->prot_hook.func = (po->rx_ring.pg_vec) ? in packet_set_ring()
4500 if (atomic_read(&po->mapped)) in packet_set_ring()
4502 atomic_read(&po->mapped)); in packet_set_ring()
4504 mutex_unlock(&po->pg_vec_lock); in packet_set_ring()
4506 spin_lock(&po->bind_lock); in packet_set_ring()
4508 WRITE_ONCE(po->num, num); in packet_set_ring()
4511 spin_unlock(&po->bind_lock); in packet_set_ring()
4512 if (pg_vec && (po->tp_version > TPACKET_V2)) { in packet_set_ring()
4513 /* Because we don't support block-based V3 on tx-ring */ in packet_set_ring()
4521 free_pg_vec(pg_vec, order, req->tp_block_nr); in packet_set_ring()
4530 struct sock *sk = sock->sk; in packet_mmap()
4535 int err = -EINVAL; in packet_mmap()
4538 if (vma->vm_pgoff) in packet_mmap()
4539 return -EINVAL; in packet_mmap()
4541 mutex_lock(&po->pg_vec_lock); in packet_mmap()
4544 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { in packet_mmap()
4545 if (rb->pg_vec) { in packet_mmap()
4546 expected_size += rb->pg_vec_len in packet_mmap()
4547 * rb->pg_vec_pages in packet_mmap()
4555 size = vma->vm_end - vma->vm_start; in packet_mmap()
4559 start = vma->vm_start; in packet_mmap()
4560 for (rb = &po->rx_ring; rb <= &po->tx_ring; rb++) { in packet_mmap()
4561 if (rb->pg_vec == NULL) in packet_mmap()
4564 for (i = 0; i < rb->pg_vec_len; i++) { in packet_mmap()
4566 void *kaddr = rb->pg_vec[i].buffer; in packet_mmap()
4569 for (pg_num = 0; pg_num < rb->pg_vec_pages; pg_num++) { in packet_mmap()
4580 atomic_inc(&po->mapped); in packet_mmap()
4581 vma->vm_ops = &packet_mmap_ops; in packet_mmap()
4585 mutex_unlock(&po->pg_vec_lock); in packet_mmap()
4649 return seq_hlist_start_head_rcu(&net->packet.sklist, *pos); in packet_seq_start()
4655 return seq_hlist_next_rcu(v, &net->packet.sklist, pos); in packet_seq_next()
4669 IS_ENABLED(CONFIG_64BIT) ? -17 : -9, "sk"); in packet_seq_show()
4675 "%pK %-6d %-4d %04x %-5d %1d %-6u %-6u %-6lu\n", in packet_seq_show()
4677 refcount_read(&s->sk_refcnt), in packet_seq_show()
4678 s->sk_type, in packet_seq_show()
4679 ntohs(READ_ONCE(po->num)), in packet_seq_show()
4680 READ_ONCE(po->ifindex), in packet_seq_show()
4681 po->running, in packet_seq_show()
4682 atomic_read(&s->sk_rmem_alloc), in packet_seq_show()
4700 mutex_init(&net->packet.sklist_lock); in packet_net_init()
4701 INIT_HLIST_HEAD(&net->packet.sklist); in packet_net_init()
4704 if (!proc_create_net("packet", 0, net->proc_net, &packet_seq_ops, in packet_net_init()
4706 return -ENOMEM; in packet_net_init()
4714 remove_proc_entry("packet", net->proc_net); in packet_net_exit()
4715 WARN_ON_ONCE(!hlist_empty(&net->packet.sklist)); in packet_net_exit()